<?php
    include_once ("MySql.php");
    include_once ("../entity/Ticket.php");
    class TicketDA extends dal{
        public function TicketDA(){
            parent::dal();
        }
        public function validarTicket($codigo){
            $sql = "CALL SGISS_TICKET_VALIDAR('$codigo')";
            $result = parent::execute($sql);
//            echo $sql;
            while ($fila = mysqli_fetch_object($result)){
                $num_row = $fila->num_row;
            }
            return $num_row;
        }
        public function actualizarTicket($entity){
            $sql = "CALL SGISU_HISTORIAL('$entity->ID_TICKET',"
                    ."'$entity->ID_MULTA',"
                    ."'$entity->AUDUSUMODIFICACION')";
            $result = parent::execute($sql);
            if($result){
                return true;
            }else{
                return false;
            }
        }
        public function generarCodigo(){
            $sql = "CALL SGISS_TICKET_CODIGO";
            $result = parent::execute($sql);
            $codigoNuevo = 0;
            while ($fila = mysqli_fetch_object($result)){
                $codigoNuevo = $fila->codigoNuevo;
            }
            return $codigoNuevo;
        }
        public function insertar($entity){
            $sql = "CALL SGISI_TICKET('$entity->ID_TICKET',"
                    ."'$entity->ID_MULTA',"
                    ."'$entity->ACTIVIDAD',"
                    ."'$entity->AREA',"
                    ."'$entity->AUDUSUCREACION')";
            $result = parent::execute($sql);
            if($result){
                return true;
            }else{
                return false;
            }
        }
        public function buscarTicket($ID_TICKET){
            $sql = "CALL SGISS_TICKET('$ID_TICKET')";
            $result = parent::execute($sql);

//            echo $sql;
            while($fila = mysqli_fetch_object($result)){
                $obj = new Ticket();
                $obj->CODIGO = $fila->CODIGO;
                $obj->ID_TICKET = $fila->ID_TICKET;
                $obj->ID_MULTA = $fila->ID_MULTA;
                $obj->ACTIVIDAD = $fila->ACTIVIDAD;
                $obj->AUDUSUCREACION = $fila->AUDUSUCREACION;
                $obj->AUDFECCREACION = $fila->AUDFECCREACION;
                $ticket[] = $obj;
            }
            return $ticket;
        }
        public function numeroRegistro($codigo,$fecha_desde,$fecha_hasta){
            $sql = "CALL SGLSS_CUENTA_TICKET('$codigo','$fecha_desde','$fecha_hasta')";
            $result = parent::execute($sql);
            
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        public function BuscarSearch($entity, $start , $limit,$sidx,$sord){
            $sql = "CALL SGLSS_TICKET_SEARCH('$entity->CODIGO',"
                    . "'$entity->FECHADESDE',"
                    . "'$entity->FECHAHASTA',"
                    . "'$start' ,"
                    . "'$limit',"
                    . "'$sidx',"
                    . "'$sord')";
            $result = parent::execute($sql);
            $fecha = date("Y-m-d");
            $c = 0;
            while($fila = mysqli_fetch_object($result)){
                $c++;
                $obj = new Ticket();
                $obj->ITEM = $c;
                $obj->CODIGO = $fila->CODIGO;
                $obj->ID_TICKET = $fila->ID_TICKET;
                $obj->ID_MULTA = $fila->ID_MULTA;
                $obj->ACTIVIDAD = $fila->ACTIVIDAD;
                $obj->AREA = $fila->AREA;
                $obj->AUDFECCREACION = $fila->AUDFECCREACION;
                $obj->AUDUSUCREACION = $fila->AUDUSUCREACION;
                $obj->AUDFECMODIFICACION = $fila->AUDFECMODIFICACION;
                $obj->AUDUSUMODIFICACION = $fila->AUDUSUMODIFICACION;
                $obj->USUCREACION = $fila->USUCREACION;
                $obj->USUMODIFICACION = $fila->USUMODIFICACION;
                $obj->INICIO = $fila->INICIO;
                $obj->FIN = $fila->FIN;
                
                if($obj->AUDUSUCREACION != NULL && $obj->AUDUSUMODIFICACION == NULL){
                    $inicio = strtotime($fila->INICIO);
                    $fin = strtotime($fecha);
                    $diferencia = abs($inicio-$fin);
                    $dias = floor($diferencia/60/60/24);
                    if($dias < 2){
                        $obj->ESTADO = "ABIERTO";
                    }elseif($dias == 2){
                        $obj->ESTADO = "PENDIENTE";
                    }elseif($dias > 2){
                        $obj->ESTADO = "DESTIEMPO";
                    }
                }elseif($obj->AUDUSUCREACION != NULL && $obj->AUDUSUMODIFICACION != NULL){
                    $inicio = strtotime($fila->INICIO);
                    $fin = strtotime($fila->FIN);
                    $diferencia = abs($inicio-$fin);
                    $dias = floor($diferencia/60/60/24);
                    if($dias <= 2){
                        $obj->ESTADO = "ATENDIDO";
                    }elseif($dias >2){
                        $obj->ESTADO = "ATENDIDO EN DESTIEMPO";
                    }
                }
                $ticket[] = $obj;
            }
            return $ticket;
        }
    }
?>
